rm(list = ls()[!ls()%in%c("drop_attn_fails")])

## Load packages

library(tidyverse)
library(ggplot2)

#drop_attn_fails <- FALSE

if(drop_attn_fails){
  
  attn <- "_attn"
  
} else{
  
  attn <- "_full"
  
}

## Load survey data 

load(file = paste0("../working/survey_data",attn,".Rdata"))

range(as.Date(c(uk$StartDate, us$StartDate, de$StartDate, uk$EndDate, us$EndDate, de$EndDate)), na.rm = TRUE)

uk_tmp <- uk %>% select((starts_with("pers_rep") | starts_with("sub_rep") | starts_with("desc_rep") | starts_with("surr_rep") | starts_with("just_rep") | starts_with("respons_rep")) & contains("num"))

us_tmp <- us %>% select((starts_with("pers_rep") | starts_with("sub_rep") | starts_with("desc_rep") | starts_with("surr_rep") | starts_with("just_rep") | starts_with("respons_rep")) & contains("num"))

de_tmp <- de %>% select((starts_with("pers_rep") | starts_with("sub_rep") | starts_with("desc_rep") | starts_with("surr_rep") | starts_with("just_rep") | starts_with("respons_rep")) & contains("num"))

de_tmp$Country <- "DE"
uk_tmp$Country <- "UK"
us_tmp$Country <- "US"

all <- bind_rows(de_tmp, uk_tmp, us_tmp)



## Create indices per dimension

all <- all %>% transmute(Country = Country, 
                  Substantive = (sub_rep_1_1_num + sub_rep_1_2_num + sub_rep_1_3_num + sub_rep_1_4_num)/4,
                 Descriptive = (desc_rep_1_1_num + desc_rep_1_2_num + desc_rep_1_3_num + desc_rep_1_4_num + desc_rep_1_5_num)/5,
                 Surrogation = ((6-surr_rep_1_1_num) + (6-surr_rep_1_2_num) + (6-surr_rep_1_3_num))/3,
                 Justification = (just_rep1_1_num + just_rep1_2_num + just_rep1_3_num + just_rep1_4_num)/4,
                 Personalization = ((6-pers_rep1_1_num) + (6-pers_rep1_2_num) + pers_rep1_3_num + pers_rep1_4_num)/4,
                 Responsiveness = ((6-respons_rep1_1_num) + respons_rep1_2_num + respons_rep1_3_num)/3
                 )

all %>% pivot_longer(cols = -Country) %>%
  mutate(name = factor(name, levels = c("Substantive", "Descriptive", "Surrogation", "Justification", "Personalization", "Responsiveness"))) %>%
  ggplot(aes(x = value, group = Country, fill = Country)) + 
  geom_density(adjust=1.5, alpha=.4) +
  geom_vline(aes(xintercept=3),
             color="black", linetype="dashed", size=1) + 
  facet_wrap(~name, ncol = 3) + 
  theme(legend.position = "bottom") + 
  theme_bw() + 
  xlab("Index") + 
  ylab("Density")
ggsave(paste0("../output/plots/descriptives/preference_densities",attn,".pdf"), height = 5, width = 10)

all %>% pivot_longer(cols = -Country) %>%
  mutate(name = factor(name, levels = c("Substantive", "Descriptive", "Surrogation", "Justification", "Personalization", "Responsiveness"))) %>%
  ggplot(aes(x = value, group = Country, fill = Country)) + 
  geom_density(adjust=1.5, alpha=.4) +
  geom_vline(aes(xintercept=3),
             color="black", linetype="dashed", size=1) + 
  facet_grid(Country ~ name) + 
  theme(legend.position = "bottom") + 
  theme_bw() + 
  xlab("Average preference on item battery") + 
  ylab("Density")
ggsave(paste0("../output/plots/descriptives/preference_densities",attn,"_new.pdf"), height = 4, width = 8)

## Check majority preferences

all %>%
  group_by(Country) %>%
  summarise(Substantive = 1- ecdf(Substantive)(3),
            Descriptive = ecdf(Descriptive)(3),
            Personalization = 1- ecdf(Personalization)(3),
            Justification = 1- ecdf(Justification)(3),
            Responsiveness = ecdf(Responsiveness)(3),
            Surrogation = ecdf(Surrogation)(3)) 

